package gw.com.android.ui.trade;

import android.os.Bundle;
import android.view.View;

import com.gwtsz.gts2.hx.R;

import gw.com.android.app.AppContances;
import gw.com.android.ui.trade.Fragment.BaseOrderFragment;
import gw.com.android.ui.trade.Fragment.OrderFailFragment;
import gw.com.android.ui.trade.Fragment.OrderSucFragment;
import gw.com.android.ui.trade.Fragment.PendingOrderModifyFragment;
import www.com.library.app.FragmentsManagerUtil;
import www.com.library.app.Logger;
import www.com.library.model.DataItemDetail;
import www.com.library.view.BtnClickListener;

/**
 * 挂单改单界面
 *
 * @author Administrator
 */
public class PendingOrderModifyActivity extends TradeOrderActivity {

	private PendingOrderModifyFragment mModifyOrderFragment;

	@Override
	protected void setAcitivityParam() {
		super.setAcitivityParam();
		if (getIntent() != null) {
			Bundle bundle = getIntent().getBundleExtra("params");
			mUiCode = bundle.getInt("uiCode", 0);
		}
	}

	public void initViewData() {

	}

	@Override
	public void initLayoutView() {
        super.initLayoutView();
		//mTitleBar.setLeftResource(R.string.order_type_pending);
		mTitleBar.setAppTitle(R.string.order_type_modity_pending);
	}

	/**
	 * 返回bundle对象
	 */
	private Bundle getBundle() {
		Bundle bundle = getIntent().getBundleExtra("params");
		return bundle;
	}

	/**
	 * 展示设置止损止盈界面
	 */
	public void showOrderFragment() {
		super.showOrderFragment();
		Logger.i("showModifyOrderFragment = " + System.currentTimeMillis());
		mTypeTag = AppContances.TAB_ID_PENDING_MODIFY_ORDER;
		mCurrentTag = AppContances.TAB_ID_PENDING_MODIFY_ORDER;
		mModifyOrderFragment = (PendingOrderModifyFragment) FragmentsManagerUtil.instance().getFragmentbyTag(this, mCurrentTag);
		if (null == mModifyOrderFragment) {
			mModifyOrderFragment = new PendingOrderModifyFragment();
			mModifyOrderFragment.setArguments(getBundle());
			mModifyOrderFragment.setOrderStatusListener(new BaseOrderFragment.onOrderStatusListener() {

				@Override
				public void onSuc(DataItemDetail mDetail) {
					mDetailItem.clear().append(mDetail);
					mHandler.sendEmptyMessage(AppContances.HANDLER_RESULT_SUC);
				}

				@Override
				public void onFail(DataItemDetail mDetail) {
					mDetailItem.clear().append(mDetail);
					mHandler.sendEmptyMessage(AppContances.HANDLER_SERVER_FAIL);
				}

				@Override
				public void onFail() {
					hideLoading();
				}

				@Override
				public void onBegin() {
					if (mCurrentTag == AppContances.TAB_ID_SUC_ORDER && mCurrentTag == AppContances.TAB_ID_FAILURE_ORDER) {
						return;
					}
					showLoading();
				}

				@Override
				public void onError(String errorMessage) {
					if (!"".equals(errorMessage) && (mCurrentTag != AppContances.TAB_ID_FAILURE_ORDER && mCurrentTag != AppContances.TAB_ID_SUC_ORDER)) {
						m_errorTextView.setText(errorMessage);
						m_errorTextView.setVisibility(View.VISIBLE);
					} else {
						m_errorTextView.setVisibility(View.GONE);
					}
				}

				@Override
				public void onShowView() {
					mProgressLayout.setVisibility(View.GONE);
				}
			});
			FragmentsManagerUtil.instance().addPushMsgTabFragment(this, mModifyOrderFragment, mCurrentTag);
		}
		showFragment(mModifyOrderFragment);
	}

	/**
	 * 展示下单失败界面
	 */

	@Override
	public void showOrderFailFragment(final DataItemDetail mDetail) {
		super.showOrderFailFragment(mDetail);
		mCurrentTag = AppContances.TAB_ID_FAILURE_ORDER;
		mFailFragment = (OrderFailFragment) FragmentsManagerUtil.instance().getFragmentbyTag(this, mCurrentTag);
		if (null == mFailFragment) {
			mFailFragment = new OrderFailFragment();
			Bundle bundle = new Bundle();
			bundle.putParcelable("mDetail", mDetail);
			mFailFragment.setArguments(bundle);
			mFailFragment.setBtnClickListener(new BtnClickListener() {

				@Override
				public void onBtnClick(int viewID) {
					showOrderFragment();
				}
			});
			FragmentsManagerUtil.instance().addPushMsgTabFragment(this, mFailFragment, mCurrentTag);
		} else {
			mFailFragment.setData(mDetail);
		}
		m_errorTextView.setVisibility(View.GONE);
		showFragment(mFailFragment);
	}

	/**
	 * 展示下单成功界面
	 */

	@Override
	public void showOrderSucFragment(DataItemDetail mDetail) {
		super.showOrderSucFragment(mDetail);
		mCurrentTag = AppContances.TAB_ID_SUC_ORDER;
		mSucFragment = (OrderSucFragment) FragmentsManagerUtil.instance().getFragmentbyTag(this, mCurrentTag);
		if (null == mSucFragment) {
			mSucFragment = new OrderSucFragment();
			Bundle bundle = new Bundle();
			bundle.putParcelable("mDetail", mDetail);
			mSucFragment.setArguments(bundle);
			FragmentsManagerUtil.instance().addPushMsgTabFragment(this, mSucFragment, mCurrentTag);
		} else {
			mSucFragment.setSucData(mDetail);
		}
		m_errorTextView.setVisibility(View.GONE);
		showFragment(mSucFragment);
	}

	@Override
	public void onSymbolNotify(DataItemDetail tickModel) {
		super.onSymbolNotify(tickModel);
		if(null != mModifyOrderFragment){
			mModifyOrderFragment.onSymbolNotify(tickModel);
		}
	}

	@Override
	public void onSymbolUpdateNotify() {
		if(null != mModifyOrderFragment){
			mModifyOrderFragment.onSymbolNotify();
		}
	}

}
